// pages/my/replaceProduct/addOldGood.js

import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
  data: {
    detailPicUrls: ['../../../image/addPic.png'],
    agentId:'',
    accountId:'',
    name: '',
    count:1,
    picUrl: '',
    isShow: true,
    sizes: [{
      name: "",
      infos: [""],
      lastId: 0
    }],
    success:false,
    versionList:[],
    isExchange:false,
    brand:'',
    brandId:'',
    carType:'',
    carType:'',
    carTypeId:'',
    carTypeColorName:'',
    carTypeColorId:'',
    firstClassName:'',
    firstClassId:'',
    secondClassName:'',
    secondClassId:'',
    specMark:true,
    specName:'',
    specId:'',
    specColorName:'',
    specColorId:'',
    stockPrice:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
    })
  },
  onShow: function () {

  },
  //获取商品名称
  goodNameTap: function(event) {
    var value = event.detail.value;
    this.setData({
      name: value
    })
  },
  add(e){
    let count = e.currentTarget.dataset.count
    console.log(count)
    count++
    this.setData({
      count:count
    })
  },
  sub(e){
    let count = e.currentTarget.dataset.count
    count--
    if (count>=1) {
      this.setData({
        count:count
      })
    }
  },
  count(e){
    console.log(e.detail.value)
    this.setData({
      count:e.detail.value
    })
  },
  addOldGoodName(e){
    console.log(e.detail.value)
    this.setData({
      ['good.name']:e.detail.value
    })
  },
  addOldGoodType(e){
    console.log(e.detail.value)
    this.setData({
      ['good.type']:e.detail.value
    })
  },
  addOldGoodPrice(e){
    console.log(e.detail.value)
    this.setData({
      stockPrice:e.detail.value
    })
  },
  addOldGoodInfo(){
    var that = this
    let imgs = this.data.detailPicUrls
    if (this.data.brandId==''||this.data.brand=='') {
      wx.showToast({
        title:'请选择品牌',
        icon:'none'
      })
    }else if (this.data.name=='') {
      wx.showToast({
        title:'请输入商品名称',
        icon:'none'
      })
    }else if (this.data.picUrl=='') {
      wx.showToast({
        title:'请上传商品图片',
        icon:'none'
      })
    }else if (this.data.carTypeId==''||this.data.carType=='') {
      wx.showToast({
        title:'请选择适用车型',
        icon:'none'
      })
    }else if (this.data.carTypeColorName==''||this.data.carTypeColorId=='') {
      wx.showToast({
        title:'请选择车型颜色',
        icon:'none'
      })
    }else if (this.data.secondClassId==''||this.data.secondClassName=='') {
      wx.showToast({
        title:'请选择商品类目',
        icon:'none'
      })
    }else if (this.data.stockPrice=='') {
      wx.showToast({
        title:'请输入旧商品价格',
        icon:'none'
      })
    }else if (this.data.specMark) {
      if (this.data.specId==''||this.data.specName=='') {
        wx.showToast({
          title:'请选择商品规格',
          icon:'none'
        })
      }else if (this.data.specColorName==''||this.data.specColorId=='') {
        wx.showToast({
          title:'请选择商品颜色',
          icon:'none'
        })
      }else{
        this.save()
      }
    }else{
      this.save()
    }
  },
  save(){
    wx.showLoading({
      title:'正在上传'
    })
    let data = {
      productType:this.data.secondClassId,//二级类目ID
      productName:this.data.name,         //商品名称
      createId:this.data.accountId,       //accountId
      agentId:this.data.agentId,          //代理商ID
      applyTo:this.data.carType+' '+this.data.carTypeColorName,
      mtId:this.data.carTypeId,           //车型ID
      mtcId:this.data.carTypeColorId,     //车型的颜色ID
      brandId:this.data.brandId,          //品牌ID
      mainImgUrl:this.data.picUrl,        //主图
      imgUrl2:this.data.detailPicUrls[0]=="../../../image/addPic.png"?'':this.data.detailPicUrls[0],
      imgUrl3:this.data.detailPicUrls[1]?this.data.detailPicUrls[1]:'',
      imgUrl4:this.data.detailPicUrls[2]?this.data.detailPicUrls[2]:'',
      imgUrl5:this.data.detailPicUrls[3]?this.data.detailPicUrls[3]:'',
      cId:this.data.specColorId,          //商品颜色ID
      versionNameId:this.data.specId,     //规格ID
      versionValues:this.data.specName,   //规格内容
      amount:this.data.count,
      accountId:this.data.accountId,
      stockPrice:this.data.stockPrice
    }
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {  
        let pages = getCurrentPages();//当前页面
        let prevPage = pages[pages.length-2];//上一页面
        let oldIds = prevPage.data.oldIds
        oldIds.push(res.data.id)
        prevPage.setData({//直接给上移页面赋值
          oldIds
        });
        wx.hideLoading()
        app.errorMsg('上传成功')
        wx.navigateBack()
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  bindSelect(e){
    let type = e.currentTarget.dataset.type
    if (type!=0) {
      if (this.data.brandId=='') {
        app.errorMsg('请选择品牌')
      }else{
        if (type==3) {
          wx.navigateTo({
            url:'../addCustomGoods/specSelect?type='+type+'&id='+this.data.secondClassId
          })
        }else{
          wx.navigateTo({
            url:'../addCustomGoods/specSelect?type='+type+'&id='+this.data.brandId+'&isOld=1'
          })
        } 
      }
    }else{
      wx.navigateTo({
        url:'../addCustomGoods/specSelect?type='+type
      })
    }
  },
    //上传商品詳情圖片
  bindUploadImg(){  
    let that = this
    wx.chooseImage({
      count:5,
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        let detailPicUrls = []
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url:appConfig.apiBase+'wx_app/image/upload',
            filePath:tempFilePaths[i],
            name:'image',
            success(res){ 
              var str = res.data.split(',')
              let code = str[0].split(':')[1]
              if (code == 1) {
                let length = str[3].length - 3
                let url = str[3].substring(13,length)
                detailPicUrls.push(url)
                console.log(detailPicUrls)
                that.setData({
                  detailPicUrls
                })
              }else{
                app.errorMsg('上传图片失败')
              }
            }
          })
        }
      }
    })
  },
  //上传商品图片
  bindUploadPic(){
    let that = this
    wx.chooseImage({
      success(res){
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url:appConfig.apiBase+'wx_app/image/upload',
          filePath:tempFilePaths[0],
          name:'image',
          success(res){
            console.log(res)
            var str = res.data.split(',')
            let code = str[0].split(':')[1]
            if (code == 1) {
              let length = str[3].length - 3
              let url = str[3].substring(13,length)
              that.setData({
                picUrl:url
              })
            }else{
              app.errorMsg(res.message)
            }
          },
          error(res){
            console.log(res)
          }
        })
      }
    })
  },
})